﻿using Aspose.Words;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Spire.Pdf.Graphics;
using WidgetsDesign.API.DataBase;
using WidgetsDesign.API.Models;
using Aspose.Cells;
using Aspose.Slides;
using System.Data;
namespace WidgetsDesign.API.Services
{
    public class UploadFileRepository : IUploadFileRepository
    {
        private readonly AppDbContext _context;
        public UploadFileRepository(AppDbContext context)
        {
            _context = context;
        }
        public void CreateFile(UploadFile uploadFile)
        {
            _context.UploadFile.Add(uploadFile);
        }
        public void DeleteFile(Guid Uuid)
        {

        }
        public async Task<bool> SaveAsync()
        {
            return (await _context.SaveChangesAsync() >= 0);
        }


        /// <summary>
        /// 转pdf
        /// </summary>
        /// <param name="url"></param>
        /// <param name="outputPdfPath"></param>
        public static void ConvertDocumentToPdf(string end, IFormFile file, string outputPdfPath)
        {

            #region key
            // new Aspose.Words.License().SetLicense(new MemoryStream(Convert.FromBase64String("PExpY2Vuc2U+CiAgPERhdGE+CiAgICA8TGljZW5zZWRUbz5TdXpob3UgQXVuYm94IFNvZnR3YXJlIENvLiwgTHRkLjwvTGljZW5zZWRUbz4KICAgIDxFbWFpbFRvPnNhbGVzQGF1bnRlYy5jb208L0VtYWlsVG8+CiAgICA8TGljZW5zZVR5cGU+RGV2ZWxvcGVyIE9FTTwvTGljZW5zZVR5cGU+CiAgICA8TGljZW5zZU5vdGU+TGltaXRlZCB0byAxIGRldmVsb3BlciwgdW5saW1pdGVkIHBoeXNpY2FsIGxvY2F0aW9uczwvTGljZW5zZU5vdGU+CiAgICA8T3JkZXJJRD4yMDA2MDIwMTI2MzM8L09yZGVySUQ+CiAgICA8VXNlcklEPjEzNDk3NjAwNjwvVXNlcklEPgogICAgPE9FTT5UaGlzIGlzIGEgcmVkaXN0cmlidXRhYmxlIGxpY2Vuc2U8L09FTT4KICAgIDxQcm9kdWN0cz4KICAgICAgPFByb2R1Y3Q+QXNwb3NlLlRvdGFsIGZvciAuTkVUPC9Qcm9kdWN0PgogICAgPC9Qcm9kdWN0cz4KICAgIDxFZGl0aW9uVHlwZT5FbnRlcnByaXNlPC9FZGl0aW9uVHlwZT4KICAgIDxTZXJpYWxOdW1iZXI+OTM2ZTVmZDEtODY2Mi00YWJmLTk1YmQtYzhkYzBmNTNhZmE2PC9TZXJpYWxOdW1iZXI+CiAgICA8U3Vic2NyaXB0aW9uRXhwaXJ5PjIwMjEwODI3PC9TdWJzY3JpcHRpb25FeHBpcnk+CiAgICA8TGljZW5zZVZlcnNpb24+My4wPC9MaWNlbnNlVmVyc2lvbj4KICAgIDxMaWNlbnNlSW5zdHJ1Y3Rpb25zPmh0dHBzOi8vcHVyY2hhc2UuYXNwb3NlLmNvbS9wb2xpY2llcy91c2UtbGljZW5zZTwvTGljZW5zZUluc3RydWN0aW9ucz4KICA8L0RhdGE+CiAgPFNpZ25hdHVyZT5wSkpjQndRdnYxV1NxZ1kyOHFJYUFKSysvTFFVWWRrQ2x5THE2RUNLU0xDQ3dMNkEwMkJFTnh5L3JzQ1V3UExXbjV2bTl0TDRQRXE1aFAzY2s0WnhEejFiK1JIWTBuQkh1SEhBY01TL1BSeEJES0NGbWg1QVFZRTlrT0FxSzM5NVBSWmJRSGowOUNGTElVUzBMdnRmVkp5cUhjblJvU3dPQnVqT1oyeDc4WFE9PC9TaWduYXR1cmU+CjwvTGljZW5zZT4=")));

            #endregion



            using (var stream = file.OpenReadStream())
            {
                switch (end)
                {

                    case "doc":
                    case "docx":
                        if (stream != null)
                        {
                            Aspose.Words.Document doc = new Aspose.Words.Document(stream);

                            doc.Save(outputPdfPath, Aspose.Words.SaveFormat.Pdf);
                        }
                        break;
                    case "ppt":
                    case "pptx":
                        // 使用Aspose.Slides处理PowerPoint文档  
                        using (Presentation pres = new Presentation(stream))
                        {
                            pres.Save(outputPdfPath, Aspose.Slides.Export.SaveFormat.Pdf);
                        }
                        break;
                    case "xls":
                    case "xlsx":
                        using (Workbook workbook = new Workbook(stream))
                        {
                            workbook.Save(outputPdfPath, Aspose.Cells.SaveFormat.Pdf);
                        }
                        break;
                    default:
                        Console.WriteLine("Unsupported file format.");
                        break;
                }
            }
        }

    }
}







